import { reactive } from 'vue'

const state = reactive({
    token: localStorage.getItem('token'),
    user: JSON.parse(localStorage.getItem('user'))
})

export default {
    get token() {
        return state.token
    },
    get user() {
        return state.user
    },
    get isAuthenticated() {
        return !!state.token
    },
    setUser(data) {
        state.token = data.token
        state.user = {
            id: data.id,
            username: data.username,
            fullName: data.fullName,
            role: data.role
        }
        localStorage.setItem('token', data.token)
        localStorage.setItem('user', JSON.stringify(state.user))
    },
    clearUser() {
        state.token = null
        state.user = null
        localStorage.removeItem('token')
        localStorage.removeItem('user')
    }
} 